Regex Detailed Examples
Below are detailed examples of using regex for SIP message manipulation:
■ | Example 1 - Number range matching and manipulation: |
● | Required manipulation: |
When the source number has prefix 30 to 40 and a digit (e.g., 3122), it needs to be changed to 2312. The last digit of the original phone number is removed (i.e., 2, leaving the number as 312) and the result is prefixed with 2.
◆ | Old header: |
To: <sip:3122@10.132.10.100;user=phone
◆ | New header: |
To: sip:2312@10.132.10.100;user=phone
● | Manipulation rule: |
Condition |
Action |
Action |
Action |
---|---|---|---|
header.to regex (<.*)([3-4][0-9])(.*)(\d)@(.*>) |
header.to |
Modify |
$1+'2'+$2+$3+'@'+$5 |
● | Explanation: |
Dialing 3122 creates the following sub-expressions:
◆ | 1: <sip: |
◆ | 2: 31 |
◆ | 3: 2 |
◆ | 4: 2 |
◆ | 5: 10.132.10.100;user=phone> |
■ | Example 2 - Manipulation based on source and destination number: |
● | Required manipulation: |
If the destination number has prefix 6, 7, or 8 (e.g., 85262146) and the source number has prefix 2001, then remove the first five digits (e.g., 85262) from the destination number and add 3 as the prefix (e.g., 3146).
◆ | Old header: |
From: <sip:20011234@10.132.10.100;user=phone>;tag=XINPYDPROEOREGEIHUHF
To: sip:85262146@10.132.10.100;user=phone
◆ | New header: |
From: <sip:20011234@company246.com;user=phone>;tag=1c13519
To: sip:3146@company244.com
● | Manipulation rules: |
Condition |
Action |
Action Type |
Action Value |
---|---|---|---|
header.to regex <sip:([6-8][1-9]{4})(.*)@(.*>) |
var.call.dst.0 |
Modify |
'3'+$2 |
header.from regex 2001 |
header.to.url.user |
Modify |
var.call.dst.0 |
● | Explanation: |
These rules are slightly complex as both the To and From headers are inspected. This rule executes
◆ | If the dialed number is prefixed with a number 6-8 (inclusive) |
◆ | If the calling party number is prefixed with 2001 |
If these conditions exist, then:
◆ | Remove the first five digits of the dialled string. |
◆ | Prefix the result with the digit 3. |
The first rule matches a dialled number that occurs in the To header (e.g., 85262146). If a match occurs, it uses a variable to store the remaining three digits and adds the digit 3 as the prefix. The second rule inspects the From header. If it contains the string 2001, then the user part of the To header is modified with the prepared variable. For example, the user (at 20011234) dials 85262146, which generates the following substring from the first rule:
◆ | $1 85262 |
◆ | $2 146 |
◆ | $3 10.132.10.100;user=phone> |
This configuration isolates the last three digits in the dialed number and prefixes them with '3'. The variable now is set to '3146'. The second rule does not use sub-expressions. It simply searches for 2001 in the From header and if there is a match the user part of the To header is manipulated using the standard manipulation syntax.
■ | Example 3 - Manipulation of SDP: |
● | Manipulation required: |
To change the packet period in the SDP.
● | Manipulation rule: |
Condition |
Action Subject |
Action Type |
Action |
---|---|---|---|
body.sdp regex (.*)(a=ptime:20)(.*) |
body.sdp |
Modify |
$1+'a=ptime:10'+$3 |
● | Explanation: |
This rule matches everything up to the a=ptime in the SDP body as $1, and stores as $3 everything after the 0 in the ptime attribute line. This is used as the closing \r\n in the SDP body. The modify action then refers to the sub-expressions $1 and $3, but does not make use of $2, instead replacing it with a=ptime:10.
■ | Example 4 – Manipulation of SDP: |
● | Manipulation rule: |
Condition |
Action Subject |
Action Type |
Action |
---|---|---|---|
body.sdp regex (.*)(m=audio)(.*)(m=audio)(.*) |
body.sdp |
Modify |
$1+$2+$3 |
● | Explanation: |
The dollar "$" values represent each condition that is enclosed by parentheses:
◆ | (.*) = $1 |
◆ | (m=audio) = $2 |
◆ | (.*) = $3 |
◆ | (m=audio) = $4 |
◆ | (.*) = $5 |
The 'Value' field means keep $1, $2, and $3 (and remove $4 and $5). The lines in the SDP represented by each $ is shown below:
Original SDP (color-coded to denote $ values):
v=0 o=mv 1980150132 244692855 IN IP6 2600:1f16:c96:aa00:951f:f946:4ebf:ef8c s=- c=IN IP6 2600:1f16:c96:aa00:951f:f946:4ebf:ef8c t=0 0 a=group:ANAT 1 2 m=audio 11090 RTP/AVP 0 8 101 c=IN IP6 :: a=rtpmap:0 PCMU/8000 a=rtpmap:8 PCMA/8000 a=rtpmap:101 telephone-event/8000 a=fmtp:101 0-15 a=mid:1 m=audio 11090 RTP/AVP 0 8 101 c=IN IP4 0.0.0.0 a=rtpmap:0 PCMU/8000 a=rtpmap:8 PCMA/8000 a=rtpmap:101 telephone-event/8000 a=fmtp:101 0-15 a=mid:2
SDP after Manipulation:
v=0 o=mv 1980150132 244692855 IN IP6 2600:1f16:c96:aa00:951f:f946:4ebf:ef8c s=- c=IN IP6 2600:1f16:c96:aa00:951f:f946:4ebf:ef8c t=0 0 a=group:ANAT 1 2 m=audio 11090 RTP/AVP 0 8 101 c=IN IP6 :: a=rtpmap:0 PCMU/8000 a=rtpmap:8 PCMA/8000 a=rtpmap:101 telephone-event/8000 a=fmtp:101 0-15 a=mid:1